package sourismobile.MIDlet.core;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import javax.microedition.io.StreamConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;

import sourismobile.MIDlet.module.MousseCanvas;

public class SourisMobileControler extends BluetoothMIDlet {

	static final Command exitCommand = new Command("Exit", Command.EXIT, 1);
	static final Command moduleCommand = new Command("Module", Command.SCREEN, 2);


	int moduleCode = 0;
	String moduleName = null;

	StreamConnection connection;
	DataInputStream in;
	DataOutputStream out;
	ServiceSeeker service; 
	Display display;
	Form f;
	ModuleList moduleList;

	public SourisMobileControler() {
		display = Display.getDisplay(this);
		service = new ServiceSeeker(this);
		moduleList = new ModuleList(this);
		f = new Form("Souris Mobile");
	}

	public void setModule(int code, String name) {
		this.moduleCode = code;
		this.moduleName = name;
	}

	public void commandAction (Command c, Displayable d) {
		if (c == exitCommand) {
			if (in != null) {try {
				out.writeInt(0);
				in.close();
				out.close();
				connection.close();
			} catch (IOException e) {
				e.printStackTrace();
			}}
			notifyDestroyed();
		}
		else if (c == moduleCommand) {
			display.setCurrent (moduleList);
		}
	}


	public void back() {
		display.setCurrent(f);
		if (moduleCode != 0) {
			f.append("Loading " + moduleName + " module...\n");
			launchModule(moduleCode);

		}
	}

	private void launchModule(int code)  {
		try {
			switch (code) {
			case 1: // Presenter
				out.writeInt(code);
				MousseCanvas presenter = new MousseCanvas(out, this);
				display.setCurrent(presenter);
				break;
			case 2: // Mousse
				out.writeInt(code);
				MousseCanvas mousse = new MousseCanvas(out, this);
				display.setCurrent(mousse);
				break;
			case 3: //Audio
				break;

			}
		} catch (IOException e) {
			throwsError(e.getMessage());
		}
	}

	public void run() {

		display.setCurrent(f);
		f.addCommand(exitCommand);
		f.setCommandListener(this);

		f.append("Welcome on Souris Mobile !\n");
		f.append("Looking for Souris Mobile Server.\n");
		service.lookingForService();
		f.append("Service Found !\n");
		f.append("Connecting...\n");
		service.connectionToService();
		f.append("Connected ! \n");
		connection = service.getConnection();

		try {
			in  = new DataInputStream ( connection.openInputStream()  );
			out = new DataOutputStream( connection.openOutputStream() );
		} catch (IOException e) {
			throwsError(e.getMessage());
		}

		f.addCommand(moduleCommand);
		f.append("Please, Choose a module (menu)\n");
	}

	public void throwsError(String message) {
		f.append("*** " + message + " ***\n");
	}

	public void destroyApp(boolean unconditional) {
		this.setModule(0, "");
		this.back();
		try {
			if (out  != null) {
				out.writeInt(0);
				out.writeInt(0);
				out.close();
			}
			if (in!=null) in.close();
			if (connection!=null) connection.close();
		} catch (IOException e) {
		}
	}
}
